<?php
namespace jy_edu;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	//订单创建时，执行此方法，执行相关操作
	public function create($params,$uid='',$url=''){
		$this->order=$params;
		$this->order['createtime']=TIMESTAMP;
		$uid=$uid?$uid:$_SESSION['uid'];
		if($params['agetime'] || $params['sex'] || $params['name']){
			$body=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$params['mid']?:$uid,
				'name'=>trim($params['name']),
				'sex'=>trim($params['sex']),
				'agetime'=>trim($params['agetime']),
				'createtime'=>TIMESTAMP,
				'storeid'=>intval($params['storeid']),
			);
			$this->pdo_insert('body',$body);
			$bodyid=$this->pdo_insertid();
		}else{
			//判断是否已经报名班级，并且班级未结束
			$isbaoming=pdo_getcolumn('jy_edu_order',array('uniacid'=>$_SESSION['uniacid'],'bodyid'=>intval($params['bodyid']),'classid'=>intval($params['classid']),'status <='=>2),'id');
			if($isbaoming && $params['bodyid']){
				exi('该小孩已经报名该班级！请查看报名信息','error','/order/detail/id='.$isbaoming);
			}
		}
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'ordersn'=>get_ordersn($this->tablename('order'),'ordersn'),
			'mid'=>$params['mid']?:$uid,
			'money'=>$params['money'],
			'pay'=>$params['money'],
			'storeid'=>intval($params['storeid']),
			'classid'=>intval($params['classid']),
			'lesson_id'=>intval($params['lesson_id']),
			'day'=>$params['day'],
			'taocankey'=>$params['taocankey'],
			'times'=>$params['times'],
			'worker_id'=>intval($params['worker_id']),
			'remark'=>$params['remark'],
			'bodyid'=>intval($params['bodyid'])?:$bodyid,
			'diyformdata'=>$params['diyformdata'],
			'plugin_name'=>$params['plugin_name'],
			'createtime'=>TIMESTAMP,
		);
		$this->pdo_insert('order',$order);
		$order_id=$this->pdo_insertid();
		$this->order['id']=$order_id;
		$this->order['ordersn']=$order['ordersn'];
		$this->order['createtime']=$order['createtime'];
		$setting=$this->plugin_setting('basic');
		//$this->model('notice')->notice_create2class($this->order,$url);
		if($setting['adminid']){
			//$this->model('notice')->notice_create2classworker($this->order,$setting['adminid']);
		}
		$workerid=pdo_getcolumn('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($params['classid'])),'workerid');
		if($workerid){
			//$this->model('notice')->notice_create2classworker($this->order,$workerid);
		}
		$this->publish('order.create',$this->order);
		return $order_id;
	}
	
	public function get_order($order_id){
		if($this->order && $this->order['id']==$order_id){
			return $this->order;
		}
		$this->order=$this->get('order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		if($this->order['classid']){
			$this->order['class']=$this->get('class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['classid']));
			$this->order['classgroup']=$this->get('class_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['class']['group_id']));
			$this->order['classworker']=$this->get('worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['class']['workerid']));
		}
		if($this->order['lessonid']){
			$this->order['lesson']=$this->get('lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['lesson_id']));
			$this->order['lessonworker']=$this->get('worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['lesson']['workerid']));
			$this->order['class']=$this->get('class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['lesson']['classid']));
			$this->order['classgroup']=$this->get('class_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['class']['group_id']));
			$this->order['classworker']=$this->get('worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['class']['workerid']));
		}
		if($this->order['bodyid']){
			$this->order['body']=$this->get('body',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['bodyid']));
		}
		if($this->order['storeid']){
			$this->order['store']=$this->get('store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['storeid']));
			$this->order['store']['pcd']=json_decode($this->order['store']['pcd'],true);
		}
		$this->order['statustitle']=$this->order_status[$this->order['status']];
		$this->order['createtime']=date('Y-m-d H:i:s',$this->order['createtime']);
		return $this->order;
	}
	public function get_lessonlog($log_id){
		$lessonlog=$this->get('lesson_log',array('id'=>$log_id,'uniacid'=>$_SESSION['uniacid']));
		if($lessonlog['classid']){
			$lessonlog['class']=$this->get('class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['classid']));
			$lessonlog['classgroup']=$this->get('class_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['class']['group_id']));
			$lessonlog['classworker']=$this->get('worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['class']['workerid']));
		}
		if($lessonlog['lesson_id']){
			$lessonlog['lesson']=$this->get('lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['lesson_id']));
			$lessonlog['lessonworker']=$this->get('worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['lesson']['workerid']));
			$lessonlog['class']=$this->get('class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['lesson']['classid']));
			$lessonlog['classgroup']=$this->get('class_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['class']['group_id']));
			$lessonlog['classworker']=$this->get('worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['class']['workerid']));
		}
		if($lessonlog['bodyid']){
			$lessonlog['body']=$this->get('body',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['bodyid']));
		}
		if($lessonlog['storeid']){
			$lessonlog['store']=$this->get('store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lessonlog['storeid']));
			$lessonlog['store']['pcd']=json_decode($lessonlog['store']['pcd'],true);
		}
		$lessonlog['statustitle']=$this->order_status[$lessonlog['status']];
		$lessonlog['createtime']=date('Y-m-d H:i:s',$lessonlog['createtime']);
		return $lessonlog;
	}
	//订单取消
	public function order_cancel($order_id){
		$this->get_order($order_id);
		if(!$this->order || $this->order['status'] !=0){
			exi('订单不存在或者不支持取消！','error');
		}
		$this->update('order',array('status'=>4),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		return true;
	}
	//订单删除
	public function order_delete($order_id,$userdeleted=false){
		$this->get_order($order_id);
		if(!$this->order){
			exi('订单不存在或者不支持删除！','error');
		}
		if($userdeleted){
			$this->update('order',array('userdeleted'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		}else{
			$this->update('order',array('deleted'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		}
	}
	public function order_rdelete($order_id){
		$this->get_order($order_id);
		if(!$this->order || $this->order['status']){
			exi('订单不存在或者不支持删除！','error');
		}
		pdo_delete('jy_edu_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	}
	//订单恢复
	public function order_reset($order_id,$userdeleted=false){
		$this->get_order($order_id);
		if(!$this->order){
			exi('订单不存在！','error');
		}
		if($userdeleted){
			$this->update('order',array('userdeleted'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		}else{
			$this->update('order',array('deleted'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		}
	}
	public function order_cancelfh($order_id){
		$this->update('order',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
	}
	//支付
	public function order_pay($order_id,$params,$url=''){
		$this->get_order($order_id);
		if(!$this->order || $this->order['ispay']){
			exi('订单不存在或者已支付！','error');
		}
		$update=array(
				'status'=>1,
				'paytype'=>$params['paytype'],
				'paytime'=>TIMESTAMP,
		);
		if($params['paytype']=='hdfk'){
			$update['ispay']=0;
		}else{
			$update['ispay']=1;
		}
		foreach($update as $key=>$value){
			$this->order[$key]=$value;
		}
		$this->pdo_update('order',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$bodydata['isxue']=1;
		if($this->order['classid']){
			$bodydata['classid']=$this->order['classid'];
			$bodydata['storeid']=$this->order['storeid'];
		}
		if($this->order['lesson_id']){
			$bodydata['lesson_id']=$this->order['lesson_id'];
		}
		$this->pdo_update('body',$bodydata,array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['bodyid']));
		$this->pdo_update('user',array('isvip'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		$lesson=pdo_get('jy_edu_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->order['lesson_id']));
		if($lesson['istimes']==2){
			$lesson['taocan']=json_decode($lesson['taocan'],true);
			$lesson['jifen']=$lesson['taocan'][$this->order['taocankey']]['jifen'];
		}
		if($lesson['jifen']>0){
			model('core/member')->member_credit($this->order['mid'],'credit1',$lesson['jifen'],true,'jy_edu','购买课程送积分');
		}
		$setting=$this->plugin_setting('basic');
		$this->model('notice')->notice_create2class($this->order,$url,true);
		if($setting['adminid']){
			$this->model('notice')->notice_create2classworker($this->order,$setting['adminid']);
		}
		$workerid=pdo_getcolumn('jy_edu_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($params['classid'])),'workerid');
		if($workerid){
			$this->model('notice')->notice_create2classworker($this->order,$workerid);
		}
	}
	
}